package org.tinygroup.managemenu.action.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.tinygroup.commons.tools.StringUtil;
import org.tinygroup.managemenu.session.UserInfo;
import org.tinygroup.managemenu.action.BaseController;
import org.tinygroup.menuframe.manager.UserAuthManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by wangwy on 2016/7/28.
 */
@Controller
@RequestMapping("/login")
public class LoginAction extends BaseController{
    @Autowired
    private UserAuthManager userAuthManager;

    @RequestMapping("")
    public String execute(){
        return "/login/login";
    }

    @RequestMapping("/redirect")
    public String redirectLogin(){
        return "/login_redirect";
    }

    @RequestMapping("check")
    @ResponseBody
    public Map<?,?> check(HttpSession session, String userId, String userPwd){
        String msg = "用户名或密码错误！";
        int status = 0;
        if(!StringUtil.isEmpty(userId)&& !StringUtil.isEmpty(userPwd)){
            boolean result = userAuthManager.checkUser(userId,userPwd);
            if(result){
                UserInfo userInfo = new UserInfo();
                userInfo.setUserName(userId);
                msg = "登录成功!";
                session.setAttribute("userInfo", userInfo);
                status = 1;
            }
        }
        return resultMap(status,msg);
    }

    @RequestMapping("logout")
    public String logout(HttpSession session,HttpServletRequest request){
        session.removeAttribute("userInfo");
        session.invalidate();
        return "redirect:/";

    }

}
